package top.jnaw.jee.platform.model;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import java.util.ArrayList;
import java.util.List;
import top.jnaw.jee.platform.Consts;
import top.jnaw.jee.utils.Excel;
import top.jnaw.jee.utils.Log;
import top.jnaw.jee.utils.Strings;

/**
 * Copyright (C) 2018 版权所有。 <p> 文件名： 文件功能描述：
 *
 * @author lc  2018/7/18 17:35 创建内容： <p> <p> 修改标识： 修改原因：
 */
public class ExportFactory {

  public static Excel createOperation(String type, String fields,
      JSONArray ids) {

    Excel e = new Excel();

    List<Excel.Title> title = JSONObject
        .parseArray(fields, Excel.Title.class);

    List<Record> list = new ArrayList<>();
    switch (type) {
      // 报名信息
      case Consts.TYPE_SIGN:
        String cec = " ";
        if (null != ids && ids.size() > 0) {
          cec = " and u.id IN \n" + Strings.genInClause(ids);
        }
        list = Db.find("SELECT\n"
            + "\twuser.`name`,\n"
            + "\t CASE wuser.sex\n"
            + "\t WHEN 'male' THEN\n"
            + "\t '男' ELSE '女' \n"
            + "\t END 'sex',\n"
            + "\twuser.mobile,\n"
            + "\tkc.title,\n"
            + "  CASE ku.status "
            + "  WHEN 'cancel' "
            + "    THEN '已取消' "
            + "  WHEN 'checking' "
            + "    THEN '申请中' "
            + "  WHEN 'success' "
            + "    THEN '已录取' "
            + "  ELSE '' END 'status', \n"
            + "\tDATE_FORMAT(ku.create_time, '%Y-%m-%d %H:%i:%s') 'create_time'\n"
            + "FROM\n"
            + "\t`wechat_users` wuser\n"
            + "\tJOIN kcgl_user ku ON wuser.openid = ku.openid\n"
            + "\tLEFT JOIN  kcgl kc ON kc.id = ku.kid\n"
            + "\tWHERE 1=1 "
            + cec);
        break;
      default:
        Log.i("--暂不支持--");
        break;

    }
    e.addRows(title, list);
    return e;
  }
}
